<?php
namespace Admin\Controller;
use Common\Controller\BaseController;
class ReportController extends BaseController 
{
    public $action = 0;
    public $page_size = 10;

    public function _initialize()
    {
        parent::_initialize();
        self::$_action_block = 4;
        $this->assign('block',self::$_action_block);
    }

    public function add()
    {
        $this->action = 1;
        $staff = $this->getUserInfo();
        $this->assign('staff',$staff);
        $this->assign('type',$this->action);
        $this->assign('now',date('Y-m-d H:i:s'),time());
        $this->display('add');
    }

    public function save()
    {
        $post = I('post.');

        $reportModel = D('Report');
        $res =  $reportModel->addReport($post);
        if(false !== $res['status'])
        {
            $this->success($res['info'],U('Admin/Report/index'));
        }
        else
        {
            $this->error($res['info']);
        }
        exit;
    }

    public function index()
    {
        $this->action = 3;
        $reportModel = D('Report');
        $type = I('get.t',0,'intval');
        $where = ' report.del=1 ';
        if(!empty($type)){
            $where .= " and report.status={$type}";
            $this->action = 2;
        }
        $count = $reportModel
                ->join('staff on staff.staff_id=report.staff_id','LEFT')
                ->count();        //获取记录数

        vendor('Page');
        $Page  = new \Vendor\Page($count,$this->page_size);
        
        $field = "report.*,staff.staff_name ";
        $list = $reportModel->field($field)
                ->join('staff on staff.staff_id=report.staff_id','LEFT')
                ->limit($Page->firstRow.','.$Page->listRows)
                ->where($where)
                ->order('reg_time DESC')
                ->select();
        $this->assign('list',$list);
//        dump($list);exit;
        $this->assign('page',$Page->show());
        $this->assign('type',$this->action);
        $this->display('index');   //显示index页面
    }
    
   

    public function getUserInfo()
    {
        $staff_id = $this->staff_id;
        // $field = "";
        $staffModel = D('Staff');
        $staff = $staffModel->getStaffById($staff_id);
        return $staff;
    }

    public function view()
    {
        $id     = I('get.id',0,'intval');

        $reportModel  = D('Report');
        $field = " report.*,staff.*";
        $info = $reportModel
                ->field($field)
                ->join('staff on report.staff_id=staff.staff_id','LEFT')
                ->where("report.report_id={$id}")
                ->limit(1)
                ->select();
        if(empty($info)){
            $this->error('数据不存在');
        }
        $this->assign('info',$info[0]);
        $this->display('view');
    }
    
    public function edit()
    {
      $this->display('edit');
    }

    public function delete()
    {
      $this->display('delete');
    }
    
}